﻿namespace ZXW.DotNet.Common.DbUtilities
{
    using System;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;

    /// <summary>
	/// 有关数据库连接的方法。 
    /// </summary>
    public class SqlHelper : BaseDbHelper
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="name"></param>
        public SqlHelper(string name)
            : base(name)
        {
            FileName = "SqlHelper.txt";
        }

        #region 抽象类方法实现
        public override DbProviderFactory Instance()
        {
            return SqlClientFactory.Instance;
        }

        public override string GetDBNow()
        {
            return " Getdate() ";
        }

        public override string GetDBDateTime()
        {
            string commandText = " SELECT " + this.GetDBNow();
            this.Open();
            string dateTime = this.ExecuteScalar(CommandType.Text, commandText, new DbParameter[0]).ToString();
            this.Close();
            return dateTime;
        }

        public override string SqlSafe(string value)
        {
            value = value.Replace("'", "''");
            return value;
        }

        public override string PlusSign(params string[] values)
        {
            string returnValue = string.Empty;
            for (int i = 0; i < values.Length; i++)
            {
                returnValue += values[i] + " + ";
            }
            if (!String.IsNullOrEmpty(returnValue))
            {
                returnValue = returnValue.Substring(0, returnValue.Length - 3);
            }
            else
            {
                returnValue = " + ";
            }
            return returnValue;
        }

        public override string GetParameter(string parameter)
        {
            return " @" + parameter + " ";
        }

        public override DbParameter MakeInParam(string targetFiled, object targetValue)
        {
            return new SqlParameter("@" + targetFiled, targetValue);
        }

        public override DbParameter[] MakeParameters(string targetFiled, object targetValue)
        {
            DbParameter[] dbParameters = null;
            if (targetFiled != null && targetValue != null)
            {
                dbParameters = new DbParameter[1];
                dbParameters[0] = this.MakeInParam(targetFiled, targetValue);
            }
            return dbParameters;
        }

        public override DbParameter[] MakeParameters(string[] targetFileds, object[] targetValues)
        {
            DbParameter[] dbParameters = new DbParameter[0];
            if (targetFileds != null && targetValues != null)
            {
                dbParameters = new DbParameter[targetFileds.Length];
                for (int i = 0; i < targetFileds.Length; i++)
                {
                    if (targetFileds[i] != null && targetValues[i] != null)
                    {
                        dbParameters[i] = this.MakeInParam(targetFileds[i], targetValues[i]);
                    }
                }
            }
            return dbParameters;
        }

        public override DbParameter MakeParam(string paramName, DbType dbType, int size, ParameterDirection direction, object value)
        {
            SqlParameter param;

            if (size > 0)
            {
                param = new SqlParameter(paramName, (SqlDbType)dbType, size);
            }
            else
            {
                param = new SqlParameter(paramName, (SqlDbType)dbType);
            }

            param.Direction = direction;
            if (!(direction == ParameterDirection.Output && value == null))
                param.Value = value;

            return param;
        } 
        #endregion
    }
}